package com.hhw.dao;

import java.util.ArrayList;
import java.util.List;

import com.hhw.entity.Admin;
import com.hhw.entity.User;
import com.hhw.util.DBUtil;

public class AdminDao extends BaseDao{
		
	
			/*
			 *查找全部用户的方法
			 */
			public List<Admin> findAdmin(){
				List<Admin> list=new ArrayList<>();
				try{
					connection = DBUtil.getConnection();
					preparedStatement=connection.prepareStatement("select * from admin ");
					resultSet=preparedStatement.executeQuery();
					Admin r= null;
					while(resultSet.next()){
						 r= new Admin();
					     r.setAdmin_id(resultSet.getInt("admin_id"));
					     r.setAdmin_name(resultSet.getString("admin_name"));
					     r.setAdmin_password(resultSet.getString("admin_password"));
						 list.add(r);
					}
					return list;
					}catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						return null;
				    }
			}
				
	
			
		/*	判断管理员是否存在根据<name>、<password>*/
			public boolean isExist(Admin r){
				boolean flag = false;
				try {
					int count = 0;
					connection = DBUtil.getConnection();
					String sql = "select count(*) as count from admin where admin_name = ? and admin_password = ?";
					preparedStatement = connection.prepareStatement(sql);
					
					preparedStatement.setString(1, r.getAdmin_name());
					preparedStatement.setString(2,r.getAdmin_password());
					
					resultSet = preparedStatement.executeQuery();
					while (resultSet.next()) {
						
						count = resultSet.getInt("count");
					}
					if (count != 0) {
						flag = true;
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					closed();
				}
				return flag;
			}
			
			
			/*查询用户方法根据<name>*/
			public List<Admin> findAllAdmin(String admin_name){
				List<Admin> list = new ArrayList<>();
				try{
					connection = DBUtil.getConnection();
					preparedStatement=connection.prepareStatement("select * from admin where admin_name=?");
					preparedStatement.setString(2, admin_name);
					resultSet=preparedStatement.executeQuery();
					Admin r= null;
					while(resultSet.next()){
						 r= new Admin();
						 r.setAdmin_id(resultSet.getInt("admin_id"));
						 r.setAdmin_name(resultSet.getString("admin_name"));
						 r.setAdmin_password(resultSet.getString("admin_password"));
						 list.add(r);
					}
					return list;
					}catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						return null;
				    }
			}
			

			
			}
			

